﻿// Calculated values. Logic to calculate and show the values. 

function showCalculatedValues(functionName)
{
    // We show all for now. We could use the param only to show one specific calculated value if the show all is low performant.

    showTotalExposed();
    showTotTx();
    showTotExpNotIll();
    showTotCiproDist();
    showTotDoxyDist();
    showTotWell();
    showTotRecover();
}
        
function calculateTotalExposed()
{
    if(parseIntByElementId('attendedConcert') > 0 || parseIntByElementId('attendeeContact') > 0 || parseIntByElementId('symptomsPresent') > 0 || parseIntByElementId('deceased') > 0)
    {
        return parseIntByElementId('totalOccupants', 0);
    }
    else
    {
        return 0;
    }
}

function showTotalExposed()
{
    var totalExposed = calculateTotalExposed();

    showValueInElementIfNotNull('totalExposed', totalExposed);

    showTotWell();
    showValueInElementIfNotNull('totExposed', totalExposed);
    showTotExpNotIll();            
    showValueInElementIfNotNull('totExpIll', parseIntByElementId('symptomsPresent'));
    showValueInElementIfNotNull('totDeceased', parseIntByElementId('deceased'));
}

function showValueInElementIfNotNull(elementId, value)
{
    if(!isNaN(value))
    {
        document.getElementById(elementId).value = value;
    }
    else
    {
        document.getElementById(elementId).value = '';
    }
}

function showTotTx()
{
    showValueInElementIfNotNull('totTx', document.getElementById('receivedIvTreatment').value);
}

function calculateTotExpNotIll()
{
    return calculateTotalExposed() - parseIntByElementId('symptomsPresent', 0);
}

function showTotExpNotIll()
{
    showValueInElementIfNotNull('totExpNotIll', calculateTotExpNotIll());
}

function calculateTotCiproDist()
{
    return parseIntByElementId('under9years', 0) + parseIntByElementId('pregnant', 0) + parseIntByElementId('allergicDoxy', 0); 
}        

function showTotCiproDist()
{
    showValueInElementIfNotNull('totCiproDist', calculateTotCiproDist());
    showTotDoxyDist();
}        

function calculateTotDoxyDist()
{
    return calculateTotalExposed() - parseIntByElementId('totCiproDist', 0); 
}       

function showTotDoxyDist()
{
    showValueInElementIfNotNull('totDoxyDist', calculateTotDoxyDist());
}       

function calculateTotWell()
{
    var totalExposed = calculateTotalExposed();

    if(totalExposed == 0)
    {
        return parseIntByElementId('totalOccupants');
    }
    else
    {
        return 0;
    }
} 

function showTotWell()
{
    showValueInElementIfNotNull('totWell', calculateTotWell());
} 

function calculateTotRecover()
{
    return parseIntByElementId('recovered');
}

function showTotRecover()
{
    showValueInElementIfNotNull('totRecover', calculateTotRecover());
}

// Util functions

function parseIntByElementId(elementid)
{
    return parseInt(document.getElementById(elementid).value, 10);
}

function parseIntByElementId(elementid, defaultValue)
{
    var result = parseInt(document.getElementById(elementid).value, 10);
    if(isNaN(result))
    {
        return defaultValue;
    }
    
    return result;            
}
